package com.telecomyt.jwportal.utils;

import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.cache.ExternalCacheDiskCacheFactory;

import java.io.File;

/**
 * @author created by chengDa
 * @time 2020/10/16  15:14
 * @description
 */
public class GlideCacheUtil {

        private static GlideCacheUtil instance;

        public static GlideCacheUtil getInstance(){
            if(instance == null){
                instance = new GlideCacheUtil();
            }
            return instance;
        }

        /**
         * 清理图片磁盘缓存
         */
        public void clearImageDiskCache(final Context context){
            try{
                if(Looper.myLooper() == Looper.getMainLooper()){
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.get(context).clearDiskCache();
                        }
                    }).start();
                }else{
                    Glide.get(context).clearDiskCache();
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }

        /**
         * 清除图片内存缓存
         */
        public void clearImageMemoryCache(final Context context){
            try{
                if(Looper.myLooper() == Looper.getMainLooper()){
                    Glide.get(context).clearMemory();
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        /**
         * 删除指定目录下的文件，这里用于缓存的删除
         *
         * @param filePath filePath
         * @param deleteThisPath deleteThisPath
         */
        private void deleteFolderFile(String filePath, boolean deleteThisPath) {
            if (!TextUtils.isEmpty(filePath)) {
                try {
                    File file = new File(filePath);
                    if (file.isDirectory()) {
                        File files[] = file.listFiles();
                        for (File file1 : files) {
                            deleteFolderFile(file1.getAbsolutePath(), true);
                        }
                    }
                    if (deleteThisPath) {
                        if (!file.isDirectory()) {
                            file.delete();
                        } else {
                            if (file.listFiles().length == 0) {
                                file.delete();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        /**
         * 清除图片所有缓存
         * 主要调用这个方法
         */
        public void clearImageAllCache(Context context) {
            clearImageDiskCache(context);
            clearImageMemoryCache(context);
            String ImageExternalCatchDir=context.getExternalCacheDir()+ ExternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR;
            deleteFolderFile(ImageExternalCatchDir, true);
        }
    }
